#!/bin/bash
BaseDir=$(cd "$(dirname "$0")"; pwd)
cd ${BaseDir}

set -x
set -o errexit

if [ $# -eq 1 ]; then
 FILE_NAME=$1 
else
 echo "Must input image file name"
 FILE_NAME=$(hotsname)
fi

echo "${FILE_NAME}...."

#导出所有镜像，没有筛选
#docker images | awk '{print $1":"$2}'
docker images --format "{{.Repository}}:{{.Tag}}" > ${FILE_NAME}

IMG=$(cat ${FILE_NAME})
CMD="docker save"
for image in $IMG; do
    url=${image%%/*}
    ImageName=${image#*/}
    echo $image $url $ImageName

    CMD="${CMD} ${image}"
done

CMD="${CMD} ${image} -o ${FILE_NAME}.tar"
#export docker images
$CMD

echo "done." 
